package com.longlong.config.security.service;

import com.longlong.config.globalExceptionClass.BusinessException;
import com.longlong.entity.AdminUser;
import com.longlong.mapper.AdminUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 实现UserDetailsService接口，实现自定义登录逻辑
 * 重写登录方法
 */
@Service
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private AdminUserMapper adminuserMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //1.再mapper中自定义登录，根据用户名获取用户信息
        AdminUser user = adminuserMapper.findUserByName(username);
        if(user == null){
            throw new BusinessException(402,"用户名和密码有误!!!");
        }

        return user;
    }
}


























